#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp>
#include "get_arg.h"



// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib





// my functions
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <blast_results.h>
#include <codeml_results.h>

using std::string;
using std::cerr;
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::filesystem::path;
#define DEFAULT_CODEML_PATH "/net/cpp-group/tools/paml/"
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	// checked mandatory arguments
	//
//	po::options_description mandatory_args("Mandatory Arguments");
//	mandatory_args.add_options()



	args.optional_args.add_options()
	("input_file,i", 					po::value<string>(&args.file_input)
												->set_name("FILE")
												->default_value("STDIN"),
											"Sequence pair results in tab-delimited format.")
	("count_identifiers,c",				po::bool_switch(&args.count_identifiers),
											"Print the number of pairs each identifier "
											"appears in (in the second column of output).")
	("output_path,o",					po::value<string>(&args.path_output_prot)
												->set_name("PATH"),
											"If --map_prot_to_gene_to_species is specified, "
											"the identifiers of each species can be written "
											"to separate files by including the term "
											"'[SPECIES]' in the path. The species name "
											"will be substituted in its place.)")
	("self_self,s",						po::bool_switch(&args.keep_self_self),
											"Identifier pairs which are identical are "
											"otherwise ignored without this flag. (This "
											"applies to both identifiers and mapped genes.")
	("#comment,#",						po::value<string>(&args.comment_letters)
												->set_name("LETTERS")
												->default_value("#"),
											"Use one of these letters to begin a "
											"commented (ignored) line.")
	("map_prot_to_gene_to_species,m", 	po::value<string>(&args.file_ids)
												->set_name("FILE"),
											"Contains sequence identifiers in the first "
											"column, tab-separated from the second "
											"column of gene identifiers, with species "
											"names in the third.")
	("no_mapping,n",					po::bool_switch(&args.genes),
											"Default unless --map_prot_to_gene_to_species is "
											"specified. The identifiers should be listed as is and "
											"do not need to be mapped onto genes. ")
	("gene_output_path,G",				po::value<string>(&args.path_output_gene)
												->set_name("PATH"),
											"Only used if --map_prot_to_gene_to_species "
											"is specified.\n"
											"The genes of each species can be written "
											"to separate files by including the term "
											"'[SPECIES]' in the path. The species name "
											"will be substituted in its place.)");
	
	
	args.use_results_summary();
	args.add_std_options();
	po::options_description all_args("");
	all_args
//				.add(mandatory_args)
				.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.4\n"
			"\tHistory: \n\n"
			"\tv 1.0"
			"17-01-2005\1"
			"First Version\1"
			"For use in orthology pipeline.\n"
			"\tv 1.1"
			"24-08-2005\1"
			"Apparently, first non buggy version.\n"
			"\tv 1.2\1"
			"04-01-2006\1"
			"For Andreas: Hash start comment lines. Fix bugs in handling input. "
			"Upgrade to common code for sequence pair identifiers\n"
			"\tv 1.3\1"
			"17-02-2006\1"
			"Added --count_identifiers option.\n"
			"\tv 1.4\1"
			"27-03-2006\1"
			"Comments can begin with user-defined letters. Defaults to hash '#'\n"
			"\tv 1.5\1"
			"18-07-2006\1"
			"Added --self_self and defaults to ignoring identical identifier pairs, or "
			"where both identifiers are mapped to the same gene."
			"3-02-2007\1"
			"Remove references to genes and peptides. Just identifiers and mapped "
			"identifiers. "
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =	"Takes sequence pair results and counts the number "
									"of and outputs all unique identifiers.\n";

		print_usage(cerr, string(*argv), "[OPTIONS] < SEQUENCE_PAIR.RESULTS ",
							all_args, exe_description, 80);
		return false;
	}
	if (args.path_output_gene.length() && args.file_ids.empty() && !args.genes)
		throw std::runtime_error("--gene_output_path only makes sense if the identifiers are"
								 "genes (--genes) or if --map_prot_to_gene_to_species "
								 "is specied.");
//	check_required_options(vm, mandatory_args);

	args.open_err_log();


	return true;
}

